/*
 * Measure the network speed in terms of bytes/second; your parameter space
should include the loopback interface card (between 2 processes on the same node),
the TCP protocol stack, UDP, varying packet/buffer size (1B, 1KB, 64KB), and varying the
concurrency (1 thread & 2 threads). The metrics you should be measuring are
throughput (Megabits per second, Mb/sec) and latency (ms)  1*2*3*2 = 12
experiments.

 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Environment env = new Environment();
		NetworkBenchmark BNetwork;
		
		AutoHelpParser helpParser = new AutoHelpParser();
		// init option 
		CmdLineParser.Option<Integer> protocol =
				helpParser.addHelp(
							helpParser.addIntegerOption('p', "protocol"), 
							"Choose 0 for TCP or 1 for UDP.Default is 0");
		CmdLineParser.Option<String> address = 
				helpParser.addHelp(
							helpParser.addStringOption('a', "address"),
							"Loopback address. Default is 'localhost'");
		CmdLineParser.Option<String> size =
				helpParser.addHelp(
							helpParser.addStringOption('s', "size"), 
							"Block Size of the the package's buffer: B, KB or KB64. Default is B.");
		CmdLineParser.Option<Integer> thread =
				helpParser.addHelp(
							helpParser.addIntegerOption('t', "thread"), 
							"Number of thread. Default is 1. Max is 2.");
		// init help
		CmdLineParser.Option<Boolean> help = 
				helpParser.addHelp(helpParser.addBooleanOption('h',"help"), "Print this page");
		
		try {
			helpParser.parse(args);
		} catch (CmdLineParser.OptionException e) {
			helpParser.printUsage();
			e.printStackTrace();
		}
		
		if (helpParser.getOptionValue(help) != null){
			helpParser.printUsage();
			System.exit(0);
		}
		
		Integer protocolValue = helpParser.getOptionValue(protocol);
		String addrValue = helpParser.getOptionValue(address);
		String sizeValue = helpParser.getOptionValue(size);
		Integer threadValue = helpParser.getOptionValue(thread);
		
		if(protocolValue != null){
			env.set_protocol(protocolValue);			
		} 
		if(addrValue != null){
			env.set_loopbackAddr(addrValue);
		}
		if(sizeValue == null || sizeValue.equals("B")){
			env.set_bufferSize(Environment.B);
		}else if (sizeValue.equals("KB")){
			env.set_bufferSize(Environment.KB);
		}else if (sizeValue.equals("KB64")){
			env.set_bufferSize(Environment.KB64);
		}
		if(threadValue != null){
			env.set_nbThread(threadValue);			
		}
		
		BNetwork = new NetworkBenchmark(env);
		BNetwork.runTests();
	}

}
